package org.platform.processor.diagnose;

import lombok.extern.slf4j.Slf4j;
import org.platform.constant.DiagnoseConst;
import org.platform.core.JobManager;
import org.platform.dao.mapper.FdDiagnosetaskMapper;
import org.platform.mo.db.FdDiagnosetask;
import org.platform.tool.helper.LogHelper;
import org.quartz.*;
import org.quartz.impl.JobDetailImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * @author FengJie
 * @date 2018/4/23
 */
@Component
@Slf4j
public class BaseDiagnoseJob implements InterruptableJob {
    private static final String JOB_KEY = "BaseDiagnoseJob";

    @Autowired
    private FdDiagnosetaskMapper mapper;
    @Autowired
    private JobManager job;
    @Autowired
    private LogHelper logHelper;


    @Override
    public void interrupt() {

    }

    @Override
    public void execute(JobExecutionContext context) {
        try {
            Example example = new Example(FdDiagnosetask.class);
            example.createCriteria()
                    .andEqualTo("status", DiagnoseConst.DIAGNOSE_STATUS_WAIT);
            example.orderBy("ismain").desc();
            List<FdDiagnosetask> taskList = mapper.selectByExample(example);
            if (taskList.size() > 0) {
                FdDiagnosetask task = taskList.get(0);
                task.setStatus(DiagnoseConst.DIAGNOSE_STATUS_EXECUTE);
                mapper.updateByPrimaryKeySelective(task);

                IDiagnoseExecutor executor = DiagnoseFactorySingleton.getInstance().getExecutor(task.getType());
                String status;
                if (executor.doExecute(task)) {
                    status = "【成功】";
                    task.setEndtime(System.currentTimeMillis());
                    task.setStatus(DiagnoseConst.DIAGNOSE_STATUS_SUCCESS);
                    mapper.updateByPrimaryKeySelective(task);
                } else {
                    status = "【失败】";
                    task.setStatus(DiagnoseConst.DIAGNOSE_STATUS_FAILURE);
                }
                mapper.updateByPrimaryKeySelective(task);
                String content = "诊断结论: " + task.getDiagnosename() + "最终诊断状态为: " + status;
                logHelper.normal(content, task.getDiagnoseid(), task.getDeviceid());
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("定时线程【{}】发生错误,异常信息", JOB_KEY, e);
        }
    }

    // todo  1.这里的诊断信息需要优化   2.加超时机制

    @PostConstruct
    public void init() {
        JobDetailImpl jd = new JobDetailImpl();
        jd.setJobClass(BaseDiagnoseJob.class);
        jd.setKey(JobKey.jobKey(JOB_KEY));

        SimpleTrigger trigger = TriggerBuilder.newTrigger()
                .withSchedule(SimpleScheduleBuilder
                        .simpleSchedule()
                        .withIntervalInSeconds(10)
                        .repeatForever())
                .startNow()
                .build();

        job.register(jd, trigger);


    }
}
